package com.cn.jx.manager.service.mtn.Impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.common.service.BaseService;
import com.cn.jx.manager.dao.ResourceRepository;
import com.cn.jx.manager.dao.RoleResourceRepository;
import com.cn.jx.manager.form.ResourceForm;
import com.cn.jx.manager.po.Resource;
import com.cn.jx.manager.service.mtn.IResourceServiceMtn;
import com.jx.enumType.OperatEnum;
import com.jx.enumType.ResourceEnum;
import com.jx.util.ObjectTransUtil;
import com.jx.util.Utility;

/**
 * 
 * @author 姜旭
 * @version v1.11.20.2019
 * @since 
 * <p>创建时间：2019年12月1日
 * <p>功能描述：操作服务层实现
 * <p>官方网站：management/admin/index
 * <p>邮箱地址：524577360@qq.com
 */
@Service("resourceServiceMtn") 
@Transactional
public class ResourceServiceImplMtn extends BaseService implements IResourceServiceMtn {

	/**定义{@link ResourceRepository resourceDao <tt>资源Dao层接口定义</tt>}*/
	@Autowired
	private ResourceRepository resourceDao;
	/**定义{@link RoleResourceRepository roleResourceDao <tt>角色资源关系Dao层接口定义</tt>}*/
	@Autowired
	private RoleResourceRepository roleResourceDao;

	/* 保存资源 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
//	@RedisCacheRemove(key="resourceService_menu")
	public void saveOrUpdateResource(ResourceForm resource) throws Exception {
		if(Utility.isNotBlank(resource.getId())){
			Resource reso=findEntityByPkid(resource.getId(), Resource.class);//得到库中的数据
			Resource uo=ObjectTransUtil.getUpdateObj(reso, resource);
			if(uo!=null){
				uo.setOperatTime(new Date());
				uo.setOperatUser(resource.getOperatUser());
				Resource res=resourceDao.save(uo);
				//删除缓存
//				redisUtil.hdelItem("resourceService","getResourceById="+res.getId());
				// 保存日志
				logUtil.LogMsg(Resource.class, res.getId(), OperatEnum.UPDATE, resource.getOperatUser());
			}
		}else{
			Resource r=Resource.getInstance(resource.getOperatUser());
			r.setText(resource.getText());
			r.setCode(resource.getCode());
			r.setUrl(resource.getUrl());
			r.setIconCls(resource.getIconCls());
			r.setParentId(resource.getParentId());
			r.setType(resource.getType());
			r.setApply(resource.getApply());
			r.setSort(resource.getSort());
			Resource res=resourceDao.save(r);
			// 保存日志
			logUtil.LogMsg(Resource.class, res.getId(), OperatEnum.SAVE, resource.getOperatUser());
		}
	}

	/* 删除资源。更新资源删除标记 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
//	@RedisCacheRemove(key = "resourceService_menu")
	public void delResource(List<String> id, String oprator) throws Exception {
		// 批量删除
		deleteEntityByIds(id, Resource.class);
		//删除资源角色关系
		roleResourceDao.deleteRealationByResourceList(id);
		// 保存日志
		logUtil.LogMsg(Resource.class, Utility.getListToStr(id), OperatEnum.DELETE, oprator);
	}
	
	/* 物理删除资源 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void removeResource(List<String> ids, String oprator) throws Exception {
		removeEntityByIds(ids, Resource.class);
		// 保存日志
		logUtil.LogMsg(Resource.class, Utility.getListToStr(ids), OperatEnum.REMOVE, oprator);
	}
	
	/* 找回 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
//	@RedisCacheRemove(key = "resourceService_menu")
	public void recoverResource(List<String> ids, String oprator) throws Exception {
		recoverEntityById(ids, Resource.class);
		// 保存日志
		logUtil.LogMsg(Resource.class, Utility.getListToStr(ids), OperatEnum.RECOVER, oprator);
	}
	
	/* 清空 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void removeResourceAll(String opertor) throws Exception {
		removeAllEntity(Resource.class);
		// 保存日志
		logUtil.LogMsg(Resource.class, "ALL", OperatEnum.REMOVE, opertor);
	}



	/* 初始化第一次直行的时候操作初始化资源的 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void saveResourceInit(String parentcode,String parentId,List<String> codes) {
		List<ResourceEnum> list=ResourceEnum.getResourceByParent(parentcode);
		for(ResourceEnum renum:list) {
			if(!codes.contains(renum.getCode())){
				Resource r=Resource.getInstance("system");
				r.setText(renum.getText());
				r.setCode(renum.getCode());
				r.setUrl(renum.getUrl());
				r.setIconCls(renum.getIcon());
				r.setParentId(parentId);
				r.setType(renum.getType().getCode());
				r.setApply("ADMIN");
				r.setSort(renum.getSort());
				Resource res=resourceDao.saveAndFlush(r);
				saveResourceInit(res.getCode(),res.getId(),codes);
			}else{
				Resource res=resourceDao.findResourceByCode(renum.getCode());
				saveResourceInit(renum.getCode(),res.getId(),codes);
			}
		}
	}

}
